//浅拷贝
package clonable_deepcopy;

class Student implements Cloneable{
    public String id;

    public Student(String name) {
        this.id = name;
    }

    @Override
    public String toString() {
        return "Person{" +
                "id='" + id + '\'' +
                '}';
    }

    //重写Cloneable接口的克隆方法
    //throws CloneNotSupportedException表示异常
    @Override
    protected Object clone() throws CloneNotSupportedException { //返回Object类型
        return super.clone();//protected修饰，不同包中需要用super访问
    }
}

public class Clon{
    public static void main(String[] args) throws CloneNotSupportedException {
        Student person1 = new Student("1234");
        Student person2 = (Student) person1.clone();//向下转型

        System.out.println(person2);
    }
}




package clonable_deepcopy;

class Money implements Cloneable {
    public int m=100;

    @Override
    protected Object clone() throws CloneNotSupportedException {
        return super.clone();
    }
}

class Student implements Cloneable{
    public Money money = new Money();


    //深拷贝
    @Override
    protected Object clone() throws CloneNotSupportedException { //返回Object类型
        Student tmp = (Student) super.clone();//克隆了student1,使用Cloneable中的克隆   这里必须是super才行
        tmp.money = (Money) this.money.clone();//克隆了money,使用Money中的克隆
        return tmp;
    }
}

public class Clon{
    public static void main(String[] args) throws CloneNotSupportedException {
        Student student1 = new Student();
        System.out.println(student1.money.m);
        Student student2 = (Student) student1.clone();

        student1.money.m=1234;
        System.out.println(student1.money.m);
        System.out.println(student2.money.m);
    }
}
